package com.housekeeping.housekeeping.order.service.impl;

import com.housekeeping.housekeeping.order.dao.impl.OrderDaoImpl;
import com.housekeeping.housekeeping.order.entity.Order;
import com.housekeeping.housekeeping.order.service.OrderSearchServ;
import com.housekeeping.housekeeping.order.utils.PageUtils;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

public class OrderSearchServImpl implements OrderSearchServ {
    private OrderDaoImpl orderDao = new OrderDaoImpl();

    @Override
    public PageUtils<List<Order>> getStateList(int state, int page, int pageSize) throws SQLException {
        PageUtils<List<Order>> pageObj = new PageUtils<>();
        List<Order> stateList = orderDao.getStateList(state, (page - 1) * pageSize, pageSize);
        int stateCount = orderDao.getStateCount(state);

        pageObj.setData(stateList);
        //设置总条数（用于返回前端进行分页效果）
        pageObj.setTotalCount(stateCount);
        //设置当前页为当前请求页
        pageObj.setCurrentPage(page);
        return pageObj;
    }

    @Override
    public PageUtils<List<Order>> getPAndSList(String phone, int state, int page, int pageSize) throws SQLException {
        PageUtils<List<Order>> pageObj = new PageUtils<>();
        List<Order> phoneList = orderDao.getPhoneList(phone, state, (page - 1) * pageSize, pageSize);
        int stateAndPhoneCount = orderDao.getStateAndPhoneCount(state, phone);

        pageObj.setData(phoneList);
        //设置总条数（用于返回前端进行分页效果）
        pageObj.setTotalCount(stateAndPhoneCount);
        //设置当前页为当前请求页
        pageObj.setCurrentPage(page);
        return pageObj;
    }

    @Override
    public PageUtils<List<Order>> getDateList(String startTime, String endTime, int page, int pageSize) throws SQLException {
        PageUtils<List<Order>> pageObj = new PageUtils<>();

        //将时间格式的String类型，转换为sql.Date类型
        Date startDate = Date.valueOf(startTime);
        Date endDate = Date.valueOf(endTime);

        List<Order> dateList = orderDao.getDateList(startDate, endDate, (page - 1)*pageSize, pageSize);
        int dateCount = orderDao.getDateCount(startDate, endDate);
        pageObj.setData(dateList);
        pageObj.setCurrentPage(page);
        pageObj.setTotalCount(dateCount);

        return pageObj;
    }

}
